﻿//-----------------------------------------------------------------------------------
// Validation Rule Template version 1.0
//-----------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Drawing;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WebServicesTesting.Automation.WebTest.ValidationRules
{

    [System.ComponentModel.DisplayName("ImageValidation")]
    [System.ComponentModel.Description("TODO: Add a more detailed description of 'ImageValidation' validation rule.")]
    public class ImageValidation : ValidationRule
    {

        #region Constructor
        /// <summary>
        /// standard constructor
        /// </summary>
        public ImageValidation() { }
        #endregion

        #region Properties
        /*
         * Public properties on the class will be surfaced to the user in the VS UI.
         * Only string, int, and bool types are allowed.
         */

        /*
                /// <summary>
                /// Sample string property
                /// </summary>
                private string m_myStringProperty;
                [System.ComponentModel.DisplayName("My String Property")]
                [System.ComponentModel.Description("Enter a long description for this property.")]
                [System.ComponentModel.DefaultValue("MyString")]
                public string MyStringProperty
                {
                    get { return m_myStringProperty; }
                    set { m_myStringProperty = value; }
                }
        */

        /// <summary>
        /// Contains a message that is surfaced to the user
        /// </summary>
        string imagePath;

        public string ImagePath
        {
            get { return imagePath; }
            set { imagePath = value; }
        }
        #endregion //Properties

        #region Validate Method

        /// <summary>
        /// A sample utility function that performs the actual work of validating values. 
        /// This demonstrates the best practice of moving most of the code out into 
        /// it's own function so you can test it from a unit test.
        /// </summary>
        /// <param name="sInputString">String to search</param>
        /// <param name="sReason">reason search failed</param>
        /// <returns>true - value found, false - value not found, reason contains more details</returns>
        private static bool Validate(string document)
        {
            Debug.Assert(document != null);

            // TODO:  Implement your Validation code here
            throw (new NotImplementedException("ValidationRule1:MyValidationFunction not implemented !"));

            // m_message = "Additional information regarding success/failure of function";
            // return true; // true if found, false if not found
        }

        /// <summary>
        /// The Validate method is called by the web test engine when the request is complete.
        /// e contains the request and response you need to validate.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        public override void Validate(object sender, ValidationEventArgs e)
        {
            //// IservRef.StreamingServiceClient ClientHandle = new IservRef.StreamingServiceClient();

            //// ImgServcRef.StreamingServiceClient ClientHandle = new ImgServcRef.StreamingServiceClient();

            //Uri httpUrl = new Uri("http://localhost/Image/ServiceImpl.svc");
            //ServiceHost host = new ServiceHost(typeof(Buffered_Response.ServiceImpl), httpUrl);
            //host.AddServiceEndpoint(typeof(Buffered_Response.IStreamingService), new BasicHttpBinding(), "");
            //ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            //smb.HttpGetEnabled = true;
            //host.Description.Behaviors.Add(smb);
            //host.Open();
            //Buffered_Response.ServiceImpl ClientHandle = new Buffered_Response.ServiceImpl();

            //Stream OutputFile = null;

            //try
            //{
            //    OutputFile = ClientHandle.GetData("Dryden");



            //    if (OutputFile != null)
            //    {
            //        string img1_ref, img2_ref;
            //        System.Drawing.Bitmap img1 = new Bitmap(OutputFile);

            //        Bitmap img2 = new Bitmap(imagePath);
            //        bool flag = true;
            //        if (img1.Width == img2.Width && img1.Height == img2.Height)
            //        {
            //            for (int i = 0; i < img1.Width; i++)
            //            {
            //                for (int j = 0; j < img1.Height; j++)
            //                {
            //                    img1_ref = img1.GetPixel(i, j).ToString();
            //                    img2_ref = img2.GetPixel(i, j).ToString();
            //                    if (img1_ref != img2_ref)
            //                    {

            //                        flag = false;
            //                        break;
            //                    }

            //                }

            //            }

            //            OutputFile.Close();

            //            if (flag)
            //            {
            //                e.WebTest.Context["Image Validation"] = "Successfull";
                           
            //            }
            //            else
            //            {
            //                e.WebTest.Context["Image Validation"] = "Successfull";
                            
            //            }

            //        }
            //        else
            //        {
            //            e.WebTest.Context["Image Validation"] = "Image Not found";
                        
            //        }
            //    }
            //}
            //catch (FaultException<IOException> ioExcept)
            //{

            //}
            //catch (CommunicationException comExcept)
            //{

            //}
        }
        #endregion


        #region TestCode
        /*
        /// <summary>
        /// This section contains unit test that you can call to validate 
        /// that your function works correctly
        /// You should move this code to a test class.
        /// </summary>
        [TestMethod]
        public void Test_ValidationRule1_ValidationRule()
        {
            //
            //TODO:  Add valid test to confirm correct functionallity
            //
            // You should test for positive and non positive cases 
            
            // Positive test coniditions (value found)
            this.MyStringProperty = "test input";
            Assert.IsTrue(Validate("<html><body>sample html document</body></html>"));
            //
            // Non Positive Test conditions (value not found)
            this.MyStringProperty = "negative test input";
            Assert.IsFalse(Validate("<html><body>sample html document</body></html>"));
            Assert.IsTrue(Message.Compare("Expected message"), 0);
        }
        */
        #endregion
    }

}
